package com.lagou.myspringboot.initializer;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

//@HandlesTypes({WebApplicationInitializer.class})
public class MySpringServletContainerInitializer implements ServletContainerInitializer {

    public void onStartup(Set<Class<?>> webAppInitializerClasses , ServletContext servletContext) throws ServletException {

        System.out.println("初始化 MySpringServletContainerInitializer");
//        List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();
//        if (webAppInitializerClasses != null) {
//            for (Class<?> waiClass : webAppInitializerClasses) {
//                if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
//                        WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
//                    try {
//                        initializers.add((WebApplicationInitializer)
//                                ReflectionUtils.accessibleConstructor(waiClass).newInstance());
//                    } catch (Throwable ex) {
//                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
//                    }
//                }
//            }
//        }
//        if (initializers.isEmpty()) {
//            servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
//            return;
//        }
//        servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
//        AnnotationAwareOrderComparator.sort(initializers);
//        for (WebApplicationInitializer initializer : initializers) {
//            System.out.println("for--------------");
//
//            initializer.onStartup(servletContext);
//        }
        List<WebApplicationInitializer> initializers = new LinkedList();

        if (webAppInitializerClasses != null) {
            for (Class waiClass : webAppInitializerClasses) {
                if ((!(waiClass.isInterface())) && (!(Modifier.isAbstract(waiClass.getModifiers())))
                        && (WebApplicationInitializer.class.isAssignableFrom(waiClass))) {
                    try {
                        initializers.add((WebApplicationInitializer) waiClass.newInstance());
                    } catch (Throwable ex) {
                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
                    }
                }
            }
        }

        if (initializers.isEmpty()) {
            servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
            return;
        }

        servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
        AnnotationAwareOrderComparator.sort(initializers);
        for (WebApplicationInitializer initializer : initializers){
            initializer.onStartup(servletContext);
    }
    }
}
